<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 汪利东,2016-04-12 11:28
// +----------------------------------------------------------------------

namespace Admin\Controller;


class BannerController extends BaseController
{
    public function index()
    {
        $map = [];
        $keywords = '';

        $count = D('Banner')->alias('a')->where($map)->count();// 查询满足要求的总记录数
        $Page = new  \Admin\Common\Page($count, C('page.size'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $where['keywords'] = $keywords;
        foreach ($where as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $show = $Page->show();// 分页显示输出
        $this->assign('page', $show);// 赋值分页输出


        $data = D('Banner')->alias('a')
            ->where($map)
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->order('a.sort desc')
            ->select();
        $this->data = $data;

        $this->display();
    }

    public function add()
    {
        $this->display('edit');
    }

    public function edit()
    {
        $map['banner_id'] = I('get.banner_id');
        $data = M('banner')->where($map)->find();
        $this->data = $data;

        $this->display();
    }

    public function doEdit()
    {
        $banner_id = I('get.banner_id');
        $data = M('banner')->create();

        $info = admin_upload();
        if ($info) {
            $data['pic_path'] = '/uploads/' . $info['pic_path']['savepath'] . $info['pic_path']['savename'];
        }

        if (empty($banner_id)) {
            //新增
            $r = M('banner')->add($data);
            if (!empty($r)) {
                $this->success('新增成功', U('index'));
            } else {
                $this->success('新增失败');
            }
        } else {
            //编辑
            $r = M('banner')->where(['banner_id' => $banner_id])->save($data);
            if (!empty($r)) {
                $this->success('修改成功');
            } else {
                $this->success('修改失败');
            }
        }
    }

    public function del()
    {
        $banner_id = I('get.banner_id');
        $r = M('banner')->where(['banner_id' => $banner_id])->delete();
        if ($r) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败，请联系管理员');
        }
    }
}